package com.dm.service.impl;

import com.dm.client.*;
import com.dm.common.Constants;
import com.dm.common.Dto;
import com.dm.common.DtoUtil;
import com.dm.common.EmptyUtils;
import com.dm.pojo.*;
import com.dm.service.LocalItemService;
import com.dm.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LocalItemServiceImpl implements LocalItemService {
    @Resource
    private RestDmItemClient dmItemClient;
    @Resource
    private RestDmCinemaClient dmCinemaClient;
    @Resource
    private RestDmItemCommentClient dmItemCommentClient;
    @Resource
    private RestDmImageClient dmImageClient;
    @Resource
    private RestDmSchedulerClient dmSchedulerClient;
    @Resource
    private RestDmSchedulerSeatPriceClient dmSchedulerSeatPriceClient;

    /**
     * 根据商品ID获取商品详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Dto getItem(Long id) throws Exception {
        //获取商品信息
        DmItem dmItem=dmItemClient.getDmItemById(id);
        DmCinema dmCinema=dmCinemaClient.getDmCinemaById(dmItem.getCinemaId());
        //封装
        Map<String,Object> param=new HashMap<>();
        param.put("targetId",dmItem.getId());
        param.put("category", Constants.Image.ImageCategory.item);
        //获取商品图片
        DmImage dmImage=dmImageClient.getDmImageListByMap(param).get(0);
        //封装到商品详情信息
        ItemDetailVo itemDetailVo=new ItemDetailVo();
        BeanUtils.copyProperties(dmItem,itemDetailVo);
        itemDetailVo.setCinemaId(dmCinema.getId());
        itemDetailVo.setAreaId(dmCinema.getAreaId());
        itemDetailVo.setAreaName(dmCinema.getAreaName());
        itemDetailVo.setAddress(dmCinema.getAddress());
        itemDetailVo.setState(dmItem.getState().toString());
        itemDetailVo.setImgUrl(dmImage.getImgUrl());
        itemDetailVo.setLatitude(dmCinema.getLatitude());
        itemDetailVo.setLongitude(dmCinema.getLongitude());
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }

    /**
     * 根据商品Id获取商品排期
     * @param itemId
     * @return
     * @throws Exception
     */
    @Override
    public Dto getScheduler(Long itemId) throws Exception {
        Map<String,Object> param=new HashMap<>();
        param.put("itemId",itemId);
        List<DmScheduler> dmSchedulerList=dmSchedulerClient.getDmSchedulerListByMap(param);
        List<ItemSchedulerVo> itemSchedulerVos=new ArrayList<>();
        for (DmScheduler dmScheduler: dmSchedulerList) {
            ItemSchedulerVo itemSchedulerVo=new ItemSchedulerVo();
            BeanUtils.copyProperties(dmScheduler,itemSchedulerVo);
            itemSchedulerVo.setEndTime(dmScheduler.getEndTime().toString());
            itemSchedulerVo.setStartTime(dmScheduler.getStartTime().toString());
            itemSchedulerVos.add(itemSchedulerVo);
        }
        return DtoUtil.returnDataSuccess(itemSchedulerVos);
    }

    /**
     * 根据排期Id查询商品价格
     * @param schedulerId
     * @return
     * @throws Exception
     */
    @Override
    public Dto getItemPrice(Long schedulerId) throws Exception {
        Map<String,Object> param=new HashMap<>();
        param.put("scheduleId",schedulerId);
        List<DmSchedulerSeatPrice> dmSchedulerSeatPrices=dmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(param);
        List<ItemPriceVo> itemPriceVos=new ArrayList<>();
        for (DmSchedulerSeatPrice dmSchedulerSeatPrice:dmSchedulerSeatPrices) {
            ItemPriceVo itemPriceVo=new ItemPriceVo();
            BeanUtils.copyProperties(dmSchedulerSeatPrice,itemPriceVo);
            itemPriceVo.setIsHaveSeat(dmSchedulerSeatPrice.getSeatNum()>0?1:0);
            itemPriceVos.add(itemPriceVo);
        }
        return DtoUtil.returnDataSuccess(itemPriceVos);
    }

    /**
     * 根据商品id查询剧评
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Dto queryItemComment(Long id) throws Exception {
        //封装商品Id
        Map<String,Object> param=new HashMap<>();
        param.put("itemId",id);
        //根据商品Id查询剧评
        List<DmItemComment> dmItemComments=dmItemCommentClient.getDmItemCommentListByMap(param);
        List<ItemCommentVo> itemCommentVos=new ArrayList<>();
        for (DmItemComment dmItemComment:dmItemComments) {
            ItemCommentVo itemCommentVo=new ItemCommentVo();
            BeanUtils.copyProperties(dmItemComment,itemCommentVo);
            //根据用户id获取剧评用户头像
            Map<String,Object> param1=new HashMap<>();
            param1.put("targetId",dmItemComment.getUserId());
            param1.put("category", Constants.Image.ImageCategory.user);
            List<DmImage> dmImages=dmImageClient.getDmImageListByMap(param1);
            if (EmptyUtils.isNotEmpty(dmImages)){
                DmImage dmImage=dmImages.get(0);
                itemCommentVo.setImgUrl(dmImage.getImgUrl());
            }
            itemCommentVo.setCreatedTime(dmItemComment.getCreatedTime().toString());
            itemCommentVos.add(itemCommentVo);
        }
        return DtoUtil.returnDataSuccess(itemCommentVos);
    }
}
